package org.jeecg.modules.kuner.userpointstatement.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.Strings;

import org.jeecg.modules.kuner.userpointstatement.entity.SdUserPointStatement;
import org.jeecg.modules.kuner.userpointstatement.service.ISdUserPointStatementService;
import org.jeecg.modules.kuner.userpointstatement.vo.AskTakeIncomeVO;
import org.jeecg.modules.kuner.userpointstatement.vo.ModifyUserPaymentCodeVO;
import org.jeecg.modules.kuner.userpointstatement.vo.UserIncomeVO;
import org.jeecg.modules.kuner.usertakecollectionbase.service.ISdUserTakeCollectionBaseService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.simpleframework.xml.core.Validate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Description:
 * @Author: Planifolia.Van
 * @Date: 2024/10/31 10:52
 */
@Api(tags = "小程序-用户积分相关的接口")
@RestController
@RequestMapping("/api/userPointStatement")
public class UserPointStatementApiController {

    @Resource
    private ISdUserPointStatementService userPointStatementService;
    @Resource
    private ISdUserTakeCollectionBaseService userTakeCollectionBaseService;
    @Resource
    private ISysUserService sysUserService;

    /**
     * 我的收益信息
     *
     * @param userId 用户信息
     * @return 查询结果
     */
    @ApiOperation("我的收益信息")
    @GetMapping("/myIncome/{userId}")
    public Result<UserIncomeVO> myIncome(@PathVariable String userId) {
        BigDecimal incomeCount = userTakeCollectionBaseService.getIncomeCountByUserId(userId);
        BigDecimal userTodayPointCount = userPointStatementService.getTodayPointCountByUserId(userId);
        BigDecimal takeablePointCount = userPointStatementService.getTakeablePointCountByUserId(userId);
        UserIncomeVO userIncomeVO = new UserIncomeVO();
        userIncomeVO.setTookIncome(incomeCount);
        userIncomeVO.setTodayPoint(userTodayPointCount);
        userIncomeVO.setAwaitMoney(takeablePointCount);

        return Result.OK("查询成功！", userIncomeVO);
    }

    /**
     * 我的收益记录分页查询
     *
     * @param userId 用户信息
     * @return 查询结果
     */
    @ApiOperation("我的收益记录分页查询")
    @GetMapping("/myIncomePage/{userId}")
    public Result<IPage<SdUserPointStatement>> myIncomePage(@PathVariable String userId,
                                                            @RequestParam(defaultValue = "1") Integer pageNo,
                                                            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Page<SdUserPointStatement> page = new Page<>(pageNo, pageSize);
        page = userPointStatementService.page(page, new LambdaQueryWrapper<SdUserPointStatement>().eq(SdUserPointStatement::getUserId, userId).orderByDesc(SdUserPointStatement::getCreateTime));
        return Result.OK(page);
    }

    /**
     * 申请提现
     *
     * @param askTakeIncomeVO 申请提现必要的参数信息
     * @return 提现结果
     */
    @ApiOperation("申请提现")
    @PutMapping("/askTakeIncome")
    public Result<?> askTakeIncome(@RequestBody @Validate AskTakeIncomeVO askTakeIncomeVO) {
        userTakeCollectionBaseService.askTakeIncome(askTakeIncomeVO);
        return Result.OK("申请成功，等待审核！");
    }


}
